package com.tea.funct.utils.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.tea.common.utils.StringUtils;
import com.tea.funct.utils.domain.Poster;
import com.tea.funct.utils.domain.PosterItem;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 * @PACKAGE_NAME: com.tea.funct.utils.util
 * @CLASSNAME: PosterUtil
 * @DEACRIPTION: 海报工具类
 * @AUTHOR: GuoChen
 * @DATE: 2024/5/29
 * @WEEK: 周三
 **/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PosterUtil {

    public static BufferedImage generatePoster(Poster poster) {
        try {
            // 创建画布
            BufferedImage canvas = new BufferedImage(poster.getWidth(), poster.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = canvas.createGraphics();
            // 绘制背景
            if (StringUtils.isBlank(poster.getBgUrl())) { // 背景图片
                if (ObjectUtil.isNull(poster.getBgColor())) { // 检查对象是否不为null   背景颜色
                    poster.setBgColor(Color.WHITE); // 默认白色背景
                }
                g2d.setColor(poster.getBgColor()); // 背景颜色
                g2d.fillRect(0, 0, poster.getWidth(), poster.getHeight()); // 宽 高
            } else {
                Boolean isUrlImage = (StringUtils.startsWith(poster.getBgUrl(), "https://") || StringUtils.startsWith(poster.getBgUrl(), "http://"));
                BufferedImage bgImage = isUrlImage ? ImageIO.read(new URL(poster.getBgUrl())) : ImageIO.read(new File(poster.getBgUrl())); // 背景图片
                g2d.drawImage(bgImage.getScaledInstance(poster.getWidth(), poster.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
            }
            // 抗锯齿
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            // 遍历海报元素进行绘制
            List<PosterItem> items = poster.getItems(); // 海报元素
            for (PosterItem item : items) {
                switch (item.getType().getType()) {
                    case "text":
                        g2d.setColor(item.getFontColor());
                        g2d.setFont(item.getFont());
                        g2d.drawString(item.getText(), item.getX(), item.getY());
                        break;
                    case "image":
                        Boolean isUrlImage = (StringUtils.startsWith(item.getImage(), "https://") || StringUtils.startsWith(item.getImage(), "http://"));
                        BufferedImage image = isUrlImage ? ImageIO.read(new URL(item.getImage())) : ImageIO.read(new File(item.getImage()));
                        g2d.drawImage(image.getScaledInstance(item.getWidth(), item.getHeight(), Image.SCALE_SMOOTH),
                                item.getX(), item.getY(), null);
                        break;
                    case "qrcode":
                        BufferedImage qrcode = QrCodeUtil.generate(item.getText(), item.getWidth(), item.getHeight());
                        g2d.drawImage(qrcode.getScaledInstance(item.getWidth(), item.getHeight(), Image.SCALE_SMOOTH),
                                item.getX(), item.getY(), null);
                        break;
                    default:
                        throw new RuntimeException("未知海报元素类型");
                }
            }
            // 返回画布
            return canvas;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
